package apy.codecase.business.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;

import apy.codecase.App;
import apy.codecase.R;
import apy.codecase.base.BaseActivity;
import apy.codecase.greendao.AppDaoUtils;
import apy.codecase.model.AppDaoModel;
import apy.codecase.utils.AToast;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/24.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_confirm)
    Button loginConfirm;
    @BindView(R.id.login_psw)
    EditText loginPsw;
    @BindView(R.id.login_account)
    EditText loginAccount;

    @OnClick({R.id.login_confirm,R.id.login_register})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.login_confirm:
                String account = loginAccount.getText().toString().trim();
                String loginPsw = this.loginPsw.getText().toString().trim();
                AppDaoUtils appDaoUtils = new AppDaoUtils(this);
                List<AppDaoModel> appDaoModels = appDaoUtils.queryAppDaoModelByQueryBuilder2(account);
                if(appDaoModels!=null && appDaoModels.size()>0){
                    AppDaoModel appDaoModel = appDaoModels.get(0);
                    if(loginPsw.equals(appDaoModel.getAppPsw()) || account.equals(appDaoModel.getAccount())){
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        App.TICKET = appDaoModel.getTicket();
                        finish();
                    }else{
                        AToast.showTextToast("账号密码不匹配");
                    }
                }else{
                    AToast.showTextToast("账号密码不匹配");
                }

                break;
            case R.id.login_register:
                startActivity(new Intent(this,RegisterActivity.class));
                break;
            default:break;
        }
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean controlTitle() {
        return false;
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {

    }
}
